
#include <BGridLayout>
#include <BVBoxLayout>
#include <BHBoxLayout>
#include <BReadWrite>
#include <BColorDialog>
#include <BFileDialog>
#include <BImage>
#include <BPixmap>
#include <BSpacer>
#include <BStyle>

#include "TexturePanel.h"

TexturePanel::TexturePanel(BWidget* parent) : BWidget(parent)
{
	this->setTitle("Texture");
	//this->setPerch(Part_Title, 23);

	BHBoxLayout* titleLayout = new BHBoxLayout();
	{
		_collapseButton = new BButton(Graph_Minus);
		_collapseButton->setForm(Form_IconOnly);
		_collapseButton->setSizePolicy(Policy_Preferred);
		_collapseButton->setIconSize(10);

		_enableButton = new BButton();
		_enableButton->setName("enable");
		_enableButton->setForm(Form_IconAlone);
		_enableButton->setSizePolicy(Policy_Preferred);
		_enableButton->setIconSize(16);
		_enableButton->setCheckable(true);

		_nameSpacer = new BSpacer();

		_deleteButton = new BButton(Graph_Cross);
		_deleteButton->setForm(Form_IconAlone);
		_deleteButton->setSizePolicy(Policy_Preferred);
		_deleteButton->setIconSize(10);

		titleLayout->addWidget(_collapseButton);
		titleLayout->addWidget(_enableButton);
		titleLayout->addSpring();
		titleLayout->addSpacer(_nameSpacer);
		titleLayout->addSpring();
		titleLayout->addWidget(_deleteButton);
	}
	this->setLayout(titleLayout, Part_Top);

	BGridLayout* gridLayout = new BGridLayout();
	gridLayout->setColumnPolicy(0, Policy_Preferred);
	gridLayout->setColumnPolicy(2, Policy_Preferred);
	{
		BLabel* nameLabel = new BLabel("name");
		_nameEdit = new BLineEdit();

		BLabel* blendLabel = new BLabel("blend");
		_blendComboBox = new BComboBox();
		_blendComboBox->setBorder(3);
		_blendComboBox->addItem("None", BTexture::Blend_None);
		_blendComboBox->addItem("Replace", BTexture::Blend_Replace);
		_blendComboBox->addItem("Modulate", BTexture::Blend_Modulate);
		_blendComboBox->addItem("Decal", BTexture::Blend_Decal);
		_blendComboBox->addItem("Color", BTexture::Blend_Color);

		_colorButton = new BButton(Graph_Square_Fill);

		BLabel* filterLabel = new BLabel("filter");
		_filterComboBox = new BComboBox();
		_filterComboBox->setBorder(3);
		_filterComboBox->addItem("None", BTexture::Filter_None);
		_filterComboBox->addItem("Nearest", BTexture::Filter_Nearest);
		_filterComboBox->addItem("Linear", BTexture::Filter_Linear);

		BLabel* wrapLabel = new BLabel("wrap");
		_wrapComboBox = new BComboBox();
		_wrapComboBox->setBorder(3);
		_wrapComboBox->addItem("None", BTexture::Wrap_None);
		_wrapComboBox->addItem("Clamp", BTexture::Wrap_Clamp);
		_wrapComboBox->addItem("Repeat", BTexture::Wrap_Repeat);

		BLabel* mipmapLabel = new BLabel("mipmap");
		_mipmapCheckBox = new BCheckBox();

		BLabel* fileNameLabel = new BLabel("fileName");
		_fileNameEdit = new BLineEdit();
		_imageButton = new BButton(Form_IconOnly);

		gridLayout->addWidget(0, 0, nameLabel);
		gridLayout->addWidget(0, 1, _nameEdit);
		gridLayout->addWidget(1, 0, blendLabel);
		gridLayout->addWidget(1, 1, _blendComboBox);
		gridLayout->addWidget(1, 2, _colorButton);
		gridLayout->addWidget(2, 0, filterLabel);
		gridLayout->addWidget(2, 1, _filterComboBox);
		gridLayout->addWidget(3, 0, wrapLabel);
		gridLayout->addWidget(3, 1, _wrapComboBox);
		gridLayout->addWidget(4, 0, mipmapLabel);
		gridLayout->addWidget(4, 1, _mipmapCheckBox);
		gridLayout->addWidget(5, 0, fileNameLabel);
		gridLayout->addWidget(5, 1, _fileNameEdit);
		gridLayout->addWidget(5, 2, _imageButton);
	}

	BVBoxLayout* vlayout = new BVBoxLayout(this);
	vlayout->addLayout(gridLayout);

	connect(this, Signal_AssetChanged, &TexturePanel::slotAssetChanged);
	connect(_collapseButton, Signal_Clicked, &TexturePanel::slotCollapseButtonClicked);
	connect(_enableButton, Signal_Toggled, &TexturePanel::slotDisableButtonClicked);
	connect(_deleteButton, Signal_Clicked, &TexturePanel::slotDeleteButtonClicked);
	connect(_nameEdit, Signal_EditFinished, &TexturePanel::slotNameEdited);
	connect(_blendComboBox, Signal_ValueChanged, &TexturePanel::slotBlendChanged);
	connect(_colorButton, Signal_Clicked, &TexturePanel::slotColorClicked);
	connect(_filterComboBox, Signal_ValueChanged, &TexturePanel::slotFilterChanged);
	connect(_wrapComboBox, Signal_ValueChanged, &TexturePanel::slotWrapChanged);
	connect(_mipmapCheckBox, Signal_Checked, &TexturePanel::slotMipmapClicked);
	connect(_fileNameEdit, Signal_EditFinished, &TexturePanel::slotFileNameEditFinished);
	connect(_imageButton, Signal_Clicked, &TexturePanel::slotImageButtonClicked);
}
TexturePanel::~TexturePanel()
{

}

void TexturePanel::slotAssetChanged(BObject* object, const BValue& value)
{
	BTexture* texture = value;
	if (_texture != texture)
	{
		_texture = texture;
		BLayout* layout = this->layout(Part_Center);
		if (_texture)
		{
			_enableButton->setChecked(_texture->enabled());
			_nameEdit->setText(_texture->name());
			_blendComboBox->setValue(_texture->blend());
			_filterComboBox->setValue(_texture->filter());
			_wrapComboBox->setValue(_texture->wrap());
			_mipmapCheckBox->setChecked(_texture->mipmap());
			if (BImage* image = _texture.as<BImage>())
			{
				Format format = image->format();
				_fileNameEdit->setText(image->fileName());
				_imageButton->setImage(image);
			}
			layout->setEnabled(true);
			_collapseButton->setVisible(true);
		}
		else
		{
			layout->setEnabled(false);
			_collapseButton->setVisible(false);
		}
	}
}
void TexturePanel::slotCollapseButtonClicked(BObject* object, const BValue& value)
{
	BLayout* layout = this->layout(Part_Center);
	if (layout->enabled())
	{
		_collapseButton->setGraph(Graph_Plus);
		layout->setEnabled(false);
	}
	else
	{
		_collapseButton->setGraph(Graph_Minus);
		layout->setEnabled(true);
	}
}
void TexturePanel::slotDisableButtonClicked(BObject* object, const BValue& value)
{
	bool checked = value;
	_texture->setEnabled(checked);
}
void TexturePanel::slotDeleteButtonClicked(BObject* object, const BValue& value)
{
	if (_shape && _texture)
	{
		_shape->removeTexture(_texture);
		emit(Signal_Changed);
	}
}
void TexturePanel::slotNameEdited(BObject* object, const BValue& value)
{
	const BString& name = value;
	_texture->setName(name);
	emit(Signal_Changed);
}
void TexturePanel::slotBlendChanged(BObject* object, const BValue& value)
{
	BTexture::Blend blend = value;
	_texture->setBlend(blend);
	emit(Signal_Changed);
}
void TexturePanel::slotColorClicked(BObject* object, const BValue& value)
{
	BColor color = _colorButton->color();
	color = BColorDialog::ChooseColor(0, "Choose Blend Color", color);
	_texture->setBlendColor(color);
	_colorButton->setColor(color);
	emit(Signal_Changed);
}
void TexturePanel::slotFilterChanged(BObject* object, const BValue& value)
{
	BTexture::Filter filter = value;
	_texture->setFilter(filter);
	emit(Signal_Changed);
}
void TexturePanel::slotWrapChanged(BObject* object, const BValue& value)
{
	BTexture::Wrap wrap = value;
	_texture->setWrap(wrap);
	emit(Signal_Changed);
}
void TexturePanel::slotMipmapClicked(BObject* object, const BValue& value)
{
	bool mipmap = _mipmapCheckBox->checked();
	_texture->setMipmap(mipmap);
	emit(Signal_Changed);
}
void TexturePanel::slotFileNameEditFinished(BObject* object, const BValue& value)
{
	const BString& fileName = value;
	if (BImage* image = _texture.as<BImage>())
	{
		image->setFileName(fileName);
	}
}
void TexturePanel::slotImageButtonClicked(BObject* object, const BValue& value)
{
	BString fileName = BFileDialog::OpenFile("Choose Texture Image", "", "*.png,*.jpg,*.bmp,*.tif,*.jpeg,*.tiff");
	if (fileName.size())
	{
		if (BImage* image = _texture.as<BImage>())
		{
			ReadFile(fileName, image);
			image->setFileName(fileName);
			_imageButton->setImage(image);
			_fileNameEdit->setText(image->fileName());
			emit(Signal_Changed, image);
		}
	}
}

void TexturePanel::styleEvent(const BEvent& event)
{
	const BStyle* style = this->realStyle();
	if (style)
	{
		_enableButton->setIcon(style->icon("enable"));
	}
}
